from django.db import models
from Login.models import User

# Create your models here.

class Product(models.Model):
    name = models.CharField(max_length=64, verbose_name='商品名称')
    price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='现价')
    original_price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='原价')
    cover_image = models.URLField(verbose_name='封面图片')
    short_desc = models.CharField(max_length=128, blank=True, verbose_name='简要描述')
    tag = models.CharField(max_length=32, blank=True, verbose_name='标签')
    is_new = models.BooleanField(default=False, verbose_name='是否新品')
    order = models.IntegerField(default=0, verbose_name='排序')

    def __str__(self):
        return self.name

class ProductDetail(models.Model):
    product = models.OneToOneField(Product, on_delete=models.CASCADE, related_name='detail')
    detail_images = models.JSONField(default=list, verbose_name='详情图片')
    description = models.TextField(blank=True, verbose_name='详细描述')
    stock = models.IntegerField(default=0, verbose_name='库存')

    def __str__(self):
        return f"{self.product.name} 详情"

class BrowsingRecord(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='用户')
    product = models.ForeignKey(Product, on_delete=models.CASCADE, verbose_name='商品')
    quantity = models.IntegerField(default=1, verbose_name='浏览次数')
    timestamp = models.DateTimeField(auto_now_add=True, verbose_name='浏览时间')

    class Meta:
        verbose_name = '浏览记录'
        verbose_name_plural = '浏览记录'

class FavoriteRecord(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='用户')
    product = models.ForeignKey(Product, on_delete=models.CASCADE, verbose_name='商品')
    is_favorited = models.BooleanField(default=True, verbose_name='是否被收藏')
    timestamp = models.DateTimeField(auto_now_add=True, verbose_name='收藏时间')

    class Meta:
        verbose_name = '收藏记录'
        verbose_name_plural = '收藏记录'

class OrderRecord(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='用户')
    product = models.ForeignKey(Product, on_delete=models.CASCADE, verbose_name='商品')
    quantity = models.IntegerField(default=1, verbose_name='购买数量')
    price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='下单价格', default=0)
    order_no = models.CharField(max_length=64, unique=True, null=False, blank=False)
    main_order_no = models.CharField(max_length=64, null=True, blank=True, verbose_name='合并支付批次号')
    status = models.CharField(max_length=16, default='pending', verbose_name='订单状态')
    timestamp = models.DateTimeField(auto_now_add=True, verbose_name='下单时间')

    class Meta:
        verbose_name = '订单记录'
        verbose_name_plural = '订单记录'

class CartRecord(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='用户')
    product = models.ForeignKey(Product, on_delete=models.CASCADE, verbose_name='商品')
    quantity = models.IntegerField(default=1, verbose_name='数量')
    checked_out = models.BooleanField(default=False, verbose_name='是否已结算')
    timestamp = models.DateTimeField(auto_now_add=True, verbose_name='添加时间')

    class Meta:
        verbose_name = '购物车记录'
        verbose_name_plural = '购物车记录'